﻿Public Class FrmDemarrage

#Region "Champs"

    Dim Categorie As String

#End Region

#Region "Afficher TextBox"

    Private Sub AfficherTextBox(ByVal Categorie As String)

        TextBox1.Visible = True
        gbControls.Visible = True

        Select Case Categorie

            Case "Clients"
                Try
                    MySQLConnect.MonJeuDeDonnees.Tables("client").Clear()
                Catch ex As Exception

                End Try
                gbControls.Visible = True

                dgView.DataSource = Executer_Requete_Select("select * from client", "client")

                Label1.Text = "ID Client"
                Label2.Text = "Nom Client"
                Label3.Text = "Prénom Client"
                Label4.Text = "Adresse Client"
                Label5.Text = "Code Postal Client"
                Label6.Text = "Ville Client"
                Label7.Text = "Type de Client"
                LabelRech.Text = "Recherche Clients"

                TextBox2.Visible = True
                TextBox3.Visible = True
                TextBox4.Visible = True
                TextBox5.Visible = True
                TextBox6.Visible = True
                ComboBox5.Visible = False
                ComboBox6.Visible = False
                ComboBox3.Visible = False
                TextBox1.Enabled = False
                btnDetailsCom.Visible = False
                TextBoxRech.Visible = True
                Label7.Visible = True
                ComboBoxClient.Visible = True
                ComboBoxType.Visible = False

            Case "Produits"
                Try
                    MySQLConnect.MonJeuDeDonnees.Tables("produit").Clear()
                    MySQLConnect.MonJeuDeDonnees.Tables("categorie").Clear()
                    MySQLConnect.MonJeuDeDonnees.Tables("fournisseur").Clear()
                Catch ex As Exception

                End Try
                gbControls.Visible = True


                dgView.DataSource = Executer_Requete_Select("call afficherProduits()", "produit")

                MySQLConnect.MaRequete = "call selectFournisseurs()"
                MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
                MySQLConnect.MonRemplisseur.SelectCommand = MySQLConnect.MaCommandeSpecialRequete
                MySQLConnect.MonRemplisseur.Fill(MySQLConnect.MonJeuDeDonnees, "fournisseur")
                ComboBox5.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("fournisseur")
                ComboBox5.DisplayMember = "NomFournisseur"
                ComboBox5.ValueMember = "idFournisseur"

                MySQLConnect.MaRequete = "call selectCategories()"
                MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
                MySQLConnect.MonRemplisseur.SelectCommand = MySQLConnect.MaCommandeSpecialRequete
                MySQLConnect.MonRemplisseur.Fill(MySQLConnect.MonJeuDeDonnees, "categorie")
                ComboBox6.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("categorie")
                ComboBox6.DisplayMember = "LibelleCategorie"
                ComboBox6.ValueMember = "idCategorie"


                Label1.Text = "ID Produit"
                Label2.Text = "Nom Produit"
                Label3.Text = "Prix HT"
                Label4.Text = "Quantité en Stock"
                Label5.Text = "Fournisseur"
                Label6.Text = "Catégorie"
                LabelRech.Text = "Recherche par Libelle"
                LabelRech2.Text = "Recherche par Fournisseur"

                Label7.Visible = False
                TextBox2.Visible = True
                TextBox3.Visible = True
                TextBox4.Visible = True
                ComboBox5.Visible = True
                ComboBox6.Visible = True
                TextBox1.Enabled = False
                ComboBox3.Visible = False
                btnDetailsCom.Visible = False
                TextBoxRech.Visible = True
                LabelRech2.Visible = True
                TextBoxRech2.Visible = True
                ComboBoxClient.Visible = False
                Label7.Visible = False
                ComboBoxType.Visible = False

            Case "Commandes"
                Try
                    MySQLConnect.MonJeuDeDonnees.Tables("commande").Clear()
                Catch ex As Exception

                End Try
                gbControls.Visible = True
                ComboBoxType.SelectedIndex = 0

                dgView.DataSource = Executer_Requete_Select("call getDetailCommandeInterne()", "commande")

                ComboBox3.DataSource = Executer_Requete_Select("SELECT * FROM client", "client")
                ComboBox3.DisplayMember = "PrenomClient"
                ComboBox3.ValueMember = "idClient"

                Label1.Text = "ID Commande"
                Label2.Text = "Date Commande"
                Label3.Text = "Client"
                Label4.Text = ""
                Label5.Text = "Type Commande"
                Label6.Text = ""
                LabelRech.Text = ""

                TextBox1.Enabled = False
                TextBox2.Visible = True
                TextBox3.Visible = True
                TextBox4.Visible = False
                TextBox5.Visible = False
                TextBox6.Visible = False
                ComboBox5.Visible = False
                ComboBox6.Visible = False
                ComboBox3.Visible = True
                btnDetailsCom.Visible = True
                TextBoxRech.Visible = False
                ComboBoxClient.Visible = False
                Label7.Visible = False
                ComboBoxType.Visible = True

            Case "Fournisseurs"
                Try
                    MySQLConnect.MonJeuDeDonnees.Tables("fournisseur").Clear()
                Catch ex As Exception

                End Try
                gbControls.Visible = True

                MySQLConnect.MaRequete = "SELECT * FROM fournisseur"
                MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
                MySQLConnect.MonRemplisseur.SelectCommand = MySQLConnect.MaCommandeSpecialRequete
                MySQLConnect.MonRemplisseur.Fill(MySQLConnect.MonJeuDeDonnees, "fournisseur")
                dgView.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("fournisseur")

                Label1.Text = "ID Fournisseur"
                Label2.Text = "Nom Fournisseur"
                Label3.Text = "Ville Fournisseur"
                Label4.Text = "Code Postal Fournisseur"
                Label5.Text = ""
                Label6.Text = ""
                LabelRech.Text = ""

                TextBox1.Enabled = False
                TextBox2.Visible = True
                TextBox3.Visible = True
                TextBox4.Visible = True
                TextBox5.Visible = False
                TextBox6.Visible = False
                ComboBox5.Visible = False
                ComboBox6.Visible = False
                ComboBox3.Visible = False
                btnDetailsCom.Visible = False
                TextBoxRech.Visible = False
                ComboBoxClient.Visible = False
                Label7.Visible = False
                ComboBoxType.Visible = False

            Case "Categorie"
                Try
                    MySQLConnect.MonJeuDeDonnees.Tables("categorie").Clear()
                Catch ex As Exception

                End Try
                gbControls.Visible = True

                MySQLConnect.MaRequete = "SELECT * FROM categorie"
                MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
                MySQLConnect.MonRemplisseur.SelectCommand = MySQLConnect.MaCommandeSpecialRequete
                MySQLConnect.MonRemplisseur.Fill(MySQLConnect.MonJeuDeDonnees, "categorie")
                dgView.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("categorie")

                Label1.Text = "ID Catégorie"
                Label2.Text = "Libelle Catégorie"
                Label3.Text = ""
                Label4.Text = ""
                Label5.Text = ""
                Label6.Text = ""
                LabelRech.Text = ""

                TextBox1.Enabled = False
                TextBox2.Visible = True
                TextBox3.Visible = False
                TextBox4.Visible = False
                TextBox5.Visible = False
                TextBox6.Visible = False
                ComboBox5.Visible = False
                ComboBox6.Visible = False
                ComboBox3.Visible = False
                btnDetailsCom.Visible = False
                TextBoxRech.Visible = False
                ComboBoxClient.Visible = False
                Label7.Visible = False
                ComboBoxType.Visible = False

        End Select

    End Sub

    Private Sub CellClickDataGrid()
        Dim position As Integer
        Try
            If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("client") Then
                position = dgView.CurrentRow.Index
                TextBox1.Text = MySQLConnect.MonJeuDeDonnees.Tables("client").Rows(position).Item(0)
                TextBox2.Text = MySQLConnect.MonJeuDeDonnees.Tables("client").Rows(position).Item(1)
                TextBox3.Text = MySQLConnect.MonJeuDeDonnees.Tables("client").Rows(position).Item(2)
                TextBox4.Text = MySQLConnect.MonJeuDeDonnees.Tables("client").Rows(position).Item(3)
                TextBox5.Text = MySQLConnect.MonJeuDeDonnees.Tables("client").Rows(position).Item(4)
                TextBox6.Text = MySQLConnect.MonJeuDeDonnees.Tables("client").Rows(position).Item(5)
                If (ComboBoxClient.SelectedIndex = 0) Then
                    Try
                        TextBoxRech2.Text = MySQLConnect.MonJeuDeDonnees.Tables("client").Rows(position).Item(6)
                    Catch ex As Exception

                    End Try
                    Try
                        TextBoxRech2.Text = MySQLConnect.MonJeuDeDonnees.Tables("client").Rows(position).Item(7)
                    Catch ex As Exception

                    End Try

                ElseIf (ComboBoxClient.SelectedIndex = 1) Then
                    Try
                        TextBoxRech2.Text = MySQLConnect.MonJeuDeDonnees.Tables("client").Rows(position).Item(7)
                    Catch ex As Exception

                    End Try
                    Try
                        TextBoxRech2.Text = MySQLConnect.MonJeuDeDonnees.Tables("client").Rows(position).Item(6)
                    Catch ex As Exception

                    End Try

                Else

                End If

            Else
                If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("produit") Then
                    position = dgView.CurrentRow.Index
                    TextBox1.Text = MySQLConnect.MonJeuDeDonnees.Tables("produit").Rows(position).Item(0)
                    TextBox2.Text = MySQLConnect.MonJeuDeDonnees.Tables("produit").Rows(position).Item(1)
                    TextBox3.Text = MySQLConnect.MonJeuDeDonnees.Tables("produit").Rows(position).Item(2)
                    TextBox4.Text = MySQLConnect.MonJeuDeDonnees.Tables("produit").Rows(position).Item(3)
                    ComboBox5.SelectedIndex = RecupereridFournisseur(dgView.CurrentRow.Cells(4).Value) - 1
                    ComboBox6.SelectedIndex = RecupereridCategorie(dgView.CurrentRow.Cells(5).Value) - 1
                Else
                    If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("commande") Then
                        position = dgView.CurrentRow.Index
                        TextBox1.Text = MySQLConnect.MonJeuDeDonnees.Tables("commande").Rows(position).Item(0)
                        TextBox2.Text = MySQLConnect.MonJeuDeDonnees.Tables("commande").Rows(position).Item(1)
                        ComboBox3.SelectedIndex = RecupereridClient(dgView.CurrentRow.Cells(3).Value) - 1
                    Else
                        If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("fournisseur") Then
                            position = dgView.CurrentRow.Index
                            TextBox1.Text = MySQLConnect.MonJeuDeDonnees.Tables("fournisseur").Rows(position).Item(0)

                            TextBox2.Text = MySQLConnect.MonJeuDeDonnees.Tables("fournisseur").Rows(position).Item(1)
                            TextBox3.Text = MySQLConnect.MonJeuDeDonnees.Tables("fournisseur").Rows(position).Item(2)
                            TextBox4.Text = MySQLConnect.MonJeuDeDonnees.Tables("fournisseur").Rows(position).Item(3)
                        Else
                            If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("categorie") Then
                                position = dgView.CurrentRow.Index
                                TextBox1.Text = MySQLConnect.MonJeuDeDonnees.Tables("categorie").Rows(position).Item(0)
                                TextBox2.Text = MySQLConnect.MonJeuDeDonnees.Tables("categorie").Rows(position).Item(1)
                            End If
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

#End Region

#Region "Traitement Load/DataGrid_CellClick/Quitter"

    Private Sub FrmDemarrage_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        gbControls.Visible = False
        FrmConnect.Show()
    End Sub

    Private Sub QuitterToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles QuitterToolStripMenuItem.Click
        Me.Close()
    End Sub

    Private Sub ToolStripMenuItem2_Click(sender As System.Object, e As System.EventArgs) Handles ToolStripMenuItem2.Click
        FrmAPropos.Show()
    End Sub

    Private Sub btnDetailsCom_Click(sender As Object, e As EventArgs) Handles btnDetailsCom.Click
        Dim b As New FrmDetailsCom
        b.Show()
    End Sub

    Private Sub dgView_CellClick(sender As System.Object, e As System.Windows.Forms.DataGridViewCellEventArgs) Handles dgView.CellClick

        CellClickDataGrid()

    End Sub

    Public Sub ViderTB()
        TextBox1.Clear()
        TextBox2.Clear()
        TextBox3.Clear()
        TextBox4.Clear()
        TextBox5.Clear()
        TextBox6.Clear()
        TextBoxRech2.Clear()
    End Sub

#End Region

#Region "Affichage des ComboBox/TextBox et remplissage du DataGrid"

    Private Sub ClientToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ClientToolStripMenuItem.Click


        AfficherTextBox("Clients")

    End Sub

    Private Sub ProduitsToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles ProduitsToolStripMenuItem.Click


        AfficherTextBox("Produits")

    End Sub

    Private Sub CommandesToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CommandesToolStripMenuItem.Click


        AfficherTextBox("Commandes")

    End Sub

    Private Sub FournisseursToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles FournisseursToolStripMenuItem.Click


        AfficherTextBox("Fournisseurs")

    End Sub

    Private Sub CatégoriesToolStripMenuItem_Click(sender As System.Object, e As System.EventArgs) Handles CatégoriesToolStripMenuItem.Click

        AfficherTextBox("Categorie")

    End Sub

    Private Sub DétailsCommandeToolStripMenuItem_Click(sender As Object, e As EventArgs)

        AfficherTextBox("DetailsCommandes")

    End Sub
#End Region

#Region "Traitement d'ajout"

    Private Sub btnAjouter_Click(sender As System.Object, e As System.EventArgs) Handles btnAjouter.Click
        Dim tb1 As Integer

        Try
            If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("client") Then
                tb1 = GenererClePrimaire("idClient", "client")
                Dim tb2 As String
                Dim tb3 As String
                Dim tb4 As String
                Dim tb5 As Integer
                Dim tb6 As String
                Dim tb7 As String

                If btnAjouter.Text = "Ajouter" Then
                    btnAjouter.Text = "Valider !"
                    TextBox1.Text = tb1
                    TextBox2.Text = ""
                    TextBox3.Text = ""
                    TextBox4.Text = ""
                    TextBox5.Text = ""
                    TextBox6.Text = ""
                    TextBoxRech2.Text = ""
                    If (ComboBoxClient.SelectedIndex = 0) Then
                        LabelRech2.Text = "E-Mail"
                        TextBoxRech2.Visible = True
                    Else
                        LabelRech2.Text = "Poste"
                        TextBoxRech2.Visible = True
                    End If
                    btnModifier.Enabled = False
                    btnSupprimer.Enabled = False
                ElseIf TextBox1.Text <> "" Then

                    tb2 = TextBox2.Text
                    tb3 = TextBox3.Text
                    tb4 = TextBox4.Text
                    tb5 = TextBox5.Text
                    tb6 = TextBox6.Text
                    tb7 = TextBoxRech2.Text


                    Executer_Requete_Action("INSERT INTO client VALUES ('" & tb1 & "','" & tb2 & "','" & tb3 & "','" & tb4 & "','" & tb5 & "','" & tb6 & "')")
                    If (ComboBoxClient.SelectedIndex = 0) Then
                        Executer_Requete_Action("insert into internaute values ('" & tb1 & "', '" & tb7 & "')")
                    Else
                        Executer_Requete_Action("insert into employe values ('" & tb1 & "', '" & tb7 & "')")
                    End If

                    ViderTB()
                    Rafraichir("client")
                    btnAjouter.Text = "Ajouter"
                    btnModifier.Enabled = True
                    btnSupprimer.Enabled = True
                End If

            Else
                If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("produit") Then

                    Dim tb2 As String
                    Dim tb3 As Integer
                    Dim tb4 As Integer

                    tb1 = GenererClePrimaire("idProduit", "produit")
                    If btnAjouter.Text = "Ajouter" Then
                        btnAjouter.Text = "Valider !"
                        TextBox1.Text = tb1
                        TextBox2.Text = ""
                        TextBox3.Text = ""
                        TextBox4.Text = ""
                        TextBox5.Text = ""
                        TextBox6.Text = ""
                        btnModifier.Enabled = False
                        btnSupprimer.Enabled = False
                    ElseIf TextBox1.Text <> "" Then
                        tb2 = TextBox2.Text
                        tb3 = TextBox3.Text
                        tb4 = TextBox4.Text
                        Dim tb5 = RecupereridFournisseur(ComboBox5.Text)
                        Dim tb6 = RecupereridCategorie(ComboBox6.Text)

                        Executer_Requete_Action("INSERT INTO produit VALUES ('" & tb1 & "','" & tb2 & "','" & tb3 & "','" & tb4 & "','" & tb5 & "','" & tb6 & "')")
                        ViderTB()
                        Rafraichir("produit")
                        btnAjouter.Text = "Ajouter"
                        btnModifier.Enabled = True
                        btnSupprimer.Enabled = True
                    End If

                Else
                    If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("commande") Then
                        Dim tb2 As String
                        Dim tb3 = RecupereridClient(ComboBox3.Text)

                        tb1 = GenererClePrimaire("idCommande", "commande")

                        If btnAjouter.Text = "Ajouter" Then
                            btnAjouter.Text = "Valider !"
                            TextBox1.Text = tb1
                            TextBox2.Text = ""
                            TextBox3.Text = ""
                            TextBox4.Text = ""
                            TextBox5.Text = ""
                            TextBox6.Text = ""
                            btnModifier.Enabled = False
                            btnSupprimer.Enabled = False
                        ElseIf TextBox1.Text <> "" Then
                            tb2 = TextBox2.Text
                            Executer_Requete_Action("INSERT INTO commande VALUES ('" & tb1 & "','" & tb2 & "')")

                            If ComboBoxType.SelectedIndex = 0 Then

                            Else

                            End If

                            ViderTB()
                            Rafraichir("commande")
                            btnAjouter.Text = "Ajouter"
                            btnModifier.Enabled = True
                            btnSupprimer.Enabled = True

                        End If
                    Else
                        If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("fournisseur") Then
                            Dim tb2 As String
                            Dim tb3 As String
                            Dim tb4 As Integer
                            tb1 = GenererClePrimaire("idFournisseur", "fournisseur")
                            If btnAjouter.Text = "Ajouter" Then
                                btnAjouter.Text = "Valider !"
                                TextBox1.Text = tb1
                                TextBox2.Text = ""
                                TextBox3.Text = ""
                                TextBox4.Text = ""
                                TextBox5.Text = ""
                                TextBox6.Text = ""
                                btnModifier.Enabled = False
                                btnSupprimer.Enabled = False
                            ElseIf TextBox1.Text <> "" Then

                                tb2 = TextBox2.Text
                                tb3 = TextBox3.Text
                                tb4 = CInt(TextBox4.Text)
                                Executer_Requete_Action("INSERT INTO fournisseur VALUES ('" & tb1 & "','" & tb2 & "','" & tb3 & "','" & tb4 & "')")
                                ViderTB()
                                Rafraichir("fournisseur")
                                btnAjouter.Text = "Ajouter"
                                btnModifier.Enabled = True
                                btnSupprimer.Enabled = True
                            End If
                        ElseIf dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("categorie") Then
                            Dim tb2 As String
                            tb1 = GenererClePrimaire("idCategorie", "categorie")
                            If btnAjouter.Text = "Ajouter" Then
                                btnAjouter.Text = "Valider !"
                                TextBox1.Text = tb1
                                TextBox2.Text = ""
                                TextBox3.Text = ""
                                TextBox4.Text = ""
                                TextBox5.Text = ""
                                TextBox6.Text = ""
                                btnModifier.Enabled = False
                                btnSupprimer.Enabled = False
                            ElseIf TextBox1.Text <> "" Then
                                tb2 = TextBox2.Text
                                Executer_Requete_Action("INSERT INTO categorie VALUES ('" & tb1 & "','" & tb2 & "')")
                                ViderTB()
                                Rafraichir("categorie")
                                btnAjouter.Text = "Ajouter"
                                btnModifier.Enabled = True
                                btnSupprimer.Enabled = True
                            End If
                        End If
                    End If
                End If
            End If
        Catch ex As Exception
            MessageBox.Show(ex.Message)
        End Try
    End Sub

#End Region

#Region "Traitement de modification"

    Private Sub btnModifier_Click(sender As System.Object, e As System.EventArgs) Handles btnModifier.Click
        Dim tb1 As Integer = TextBox1.Text
        Dim position As Integer
        position = dgView.CurrentRow.Cells(0).Value - 1

        If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("client") Then
            Dim tb2 As String
            Dim tb3 As String
            Dim tb4 As String
            Dim tb5 As String
            Dim tb6 As String
            Dim tb7 As String
            Dim nom As String
            Dim prenom As String
            nom = MySQLConnect.MonJeuDeDonnees.Tables("client").Rows(position).Item(1)
            prenom = MySQLConnect.MonJeuDeDonnees.Tables("client").Rows(position).Item(2)

            tb1 = TextBox1.Text
            tb2 = TextBox2.Text
            tb3 = TextBox3.Text
            tb4 = TextBox4.Text
            tb5 = TextBox5.Text
            tb6 = TextBox6.Text
            tb7 = TextBoxRech2.Text

            Executer_Requete_Action("UPDATE client SET NomClient = '" & tb2 & "', PrenomClient = '" & tb3 & "', AdRueClient = '" & tb4 & "', AdVilleClient = '" & tb5 & "' WHERE (idClient = " & tb1 & ")")
            If (ComboBoxClient.SelectedIndex = 0) Then
                Executer_Requete_Action("update internaute set mail = '" & tb7 & "'")
            Else
                Executer_Requete_Action("update employe set poste = '" & tb7 & "'")
            End If
            MessageBox.Show("Le client '" & nom & " " & prenom & "' à été modifié !", "Modification", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

            Rafraichir("client")
        Else
            If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("commande") Then
                Dim tb2 = TextBox2.Text
                Dim tb3 = RecupereridClient(ComboBox3.Text)
                Dim nom As String = MySQLConnect.MonJeuDeDonnees.Tables("commande").Rows(position).Item(1)

                MySQLConnect.MaRequete = "UPDATE commande SET DateCommande = '" & tb2 & "', idCli = '" & tb3 & "' WHERE (idCommande = " & tb1 & ")"
                MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
                MySQLConnect.MaCommandeSpecialRequete.ExecuteNonQuery()
                MessageBox.Show("La commande n°" & nom & " à été modifié !", "Modification", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                Rafraichir("commande")
            Else
                If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("fournisseur") Then
                    Dim tb2 As String = TextBox2.Text
                    Dim tb3 As String = TextBox3.Text
                    Dim tb4 As Integer = TextBox4.Text
                    Dim nom As String = MySQLConnect.MonJeuDeDonnees.Tables("fournisseur").Rows(position).Item(1)

                    MySQLConnect.MaRequete = "UPDATE fournisseur SET Nomfournisseur = '" & tb2 & "', VilleFournisseur = '" & tb3 & "' , CPFournisseur = '" & tb4 & "' WHERE (idFournisseur = " & tb1 & ")"
                    MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
                    MySQLConnect.MaCommandeSpecialRequete.ExecuteNonQuery()
                    MessageBox.Show("Le Fournisseur '" & nom & "' a été modifié.", "Modification", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                    Rafraichir("fournisseur")
                Else
                    If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("categorie") Then
                        Dim tb2 As String = TextBox2.Text
                        Dim nom As String = MySQLConnect.MonJeuDeDonnees.Tables("categorie").Rows(position).Item(1)

                        MySQLConnect.MaRequete = "UPDATE categorie SET LibelleCategorie = '" & tb2 & "' WHERE (idCategorie = " & tb1 & ")"
                        MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
                        MySQLConnect.MaCommandeSpecialRequete.ExecuteNonQuery()
                        MessageBox.Show("La Catégorie '" & nom & "' a été modifié.", "Modification", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                        Rafraichir("categorie")
                    Else
                        If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("produit") Then
                            Dim tb2 As String = TextBox2.Text
                            Dim tb3 As Integer = TextBox3.Text
                            Dim tb4 As Integer = TextBox4.Text
                            Dim tb5 As Integer = RecupereridFournisseur(ComboBox5.Text)
                            Dim tb6 As Integer = RecupereridCategorie(ComboBox6.Text)
                            Dim nom As String = MySQLConnect.MonJeuDeDonnees.Tables("produit").Rows(position).Item(1)

                            MySQLConnect.MaRequete = "UPDATE produit SET LibelleProduit = '" & tb2 & "', PrixHTProduit = '" & tb3 & "', QteStockProduit = '" & tb4 & "', idFourn = '" & tb5 & "', idCat = '" & tb6 & "' WHERE (idProduit = " & tb1 & ")"
                            MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
                            MySQLConnect.MaCommandeSpecialRequete.ExecuteNonQuery()
                            MessageBox.Show("Le Produits '" & nom & "' a été modifié.", "Modification", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)

                            Rafraichir("produit")
                        End If
                    End If
                End If
            End If
        End If
    End Sub

#End Region

#Region "Traitement de suppression"

    Private Sub btnSupprimer_Click(sender As System.Object, e As System.EventArgs) Handles btnSupprimer.Click
        Dim tb1 As Integer
        If (MessageBox.Show("Êtes-vous sur de vouloir supprimer ?", "Suppression", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes) Then
            If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("client") Then
                tb1 = TextBox1.Text
                MySQLConnect.MaRequete = "DELETE FROM client WHERE idClient = " & tb1 & ""
                MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
                MySQLConnect.MaCommandeSpecialRequete.ExecuteNonQuery()
            Else
                If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("commande") Then
                    tb1 = TextBox1.Text
                    MySQLConnect.MaRequete = "DELETE FROM lignedecommande WHERE idCommande = " & tb1 & ""
                    MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
                    MySQLConnect.MaCommandeSpecialRequete.ExecuteNonQuery()
                    MySQLConnect.MaRequete = "DELETE FROM commande WHERE idCommande = " & tb1 & ""
                    MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
                    MySQLConnect.MaCommandeSpecialRequete.ExecuteNonQuery()
                Else
                    If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("fournisseur") Then
                        tb1 = TextBox1.Text
                        MySQLConnect.MaRequete = "DELETE FROM fournisseur WHERE idFournisseur = " & tb1 & ""
                        MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
                        MySQLConnect.MaCommandeSpecialRequete.ExecuteNonQuery()
                    Else
                        If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("categorie") Then
                            tb1 = TextBox1.Text
                            MySQLConnect.MaRequete = "DELETE FROM categorie WHERE idCategorie = " & tb1 & ""
                            MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
                            MySQLConnect.MaCommandeSpecialRequete.ExecuteNonQuery()
                        Else
                            If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("produit") Then
                                tb1 = TextBox1.Text
                                MySQLConnect.MaRequete = "DELETE FROM produit WHERE idProduit = " & tb1 & ""
                                MySQLConnect.MaCommandeSpecialRequete.CommandText = MySQLConnect.MaRequete
                                MySQLConnect.MaCommandeSpecialRequete.ExecuteNonQuery()
                            End If
                        End If
                    End If
                End If
            End If
        End If
    End Sub

#End Region

#Region "Génération PDF"

    Private Sub btnPDF_Click(sender As Object, e As EventArgs) Handles btnPDF.Click
        If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("client") Then
            genererpdfpuisdatatable("Clients", MySQLConnect.MonJeuDeDonnees.Tables("client"), "Clients")
        Else
            If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("commande") Then
                genererpdfpuisdatatable("Commandes", MySQLConnect.MonJeuDeDonnees.Tables("commande"), "Commandes")
            Else
                If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("fournisseur") Then
                    genererpdfpuisdatatable("Fournisseur", MySQLConnect.MonJeuDeDonnees.Tables("fournisseur"), "Fournisseurs")
                Else
                    If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("categorie") Then
                        genererpdfpuisdatatable("Categorie", MySQLConnect.MonJeuDeDonnees.Tables("categorie"), "Categories")
                    Else
                        If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("produit") Then
                            genererpdfpuisdatatable("Produit", MySQLConnect.MonJeuDeDonnees.Tables("produit"), "Produits")
                        End If
                    End If
                End If
            End If
        End If
    End Sub

#End Region

#Region "Contrôle de saisie"

    Private Sub TextBox2_TextChanged(sender As Object, e As EventArgs) Handles TextBox2.TextChanged
        If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("client") Then
            If ValidChaine(TextBox2.Text) = False Then
                ErrorProvider1.SetError(TextBox2, "Erreur !")
            Else
                ErrorProvider1.SetError(TextBox2, "")
            End If
        End If
    End Sub

    Private Sub TextBox3_TextChanged(sender As Object, e As EventArgs) Handles TextBox3.TextChanged
        If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("client") Then
            If ValidChaine(TextBox3.Text) = False Then
                ErrorProvider1.SetError(TextBox3, "Erreur !")
            Else
                ErrorProvider1.SetError(TextBox3, "")
            End If
        End If
    End Sub

    Private Sub TextBox5_TextChanged(sender As Object, e As EventArgs) Handles TextBox5.TextChanged
        If dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("client") Then
            If ValidCP(TextBox5.Text) = False Then
                ErrorProvider1.SetError(TextBox5, "Erreur !")
            Else
                ErrorProvider1.SetError(TextBox5, "")
            End If
        End If
    End Sub

#End Region

#Region "Recherche"
    Private Sub TextBoxRech_TextChanged(sender As Object, e As EventArgs) Handles TextBoxRech.TextChanged
        Try
            MySQLConnect.MonJeuDeDonnees.Tables("produit").Clear()
            MySQLConnect.MonJeuDeDonnees.Tables("categorie").Clear()
            MySQLConnect.MonJeuDeDonnees.Tables("fournisseur").Clear()
        Catch ex As Exception

        End Try

        If (dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("produit")) Then
            dgView.DataSource = Executer_Requete_Select("call rechProdMulti('" & TextBoxRech.Text & "','" & TextBoxRech2.Text & "')", "produit")

            Executer_Requete_Select("call selectFournisseurs()", "fournisseur")
            ComboBox5.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("fournisseur")
            ComboBox5.DisplayMember = "NomFournisseur"
            ComboBox5.ValueMember = "idFournisseur"

            Executer_Requete_Select("call selectCategories()", "categorie")
            ComboBox6.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("categorie")
            ComboBox6.DisplayMember = "LibelleCategorie"
            ComboBox6.ValueMember = "idCategorie"
        End If
    End Sub

    Private Sub TextBoxRech2_TextChanged(sender As Object, e As EventArgs) Handles TextBoxRech2.TextChanged
        Try
            MySQLConnect.MonJeuDeDonnees.Tables("produit").Clear()
            MySQLConnect.MonJeuDeDonnees.Tables("categorie").Clear()
            MySQLConnect.MonJeuDeDonnees.Tables("fournisseur").Clear()
        Catch ex As Exception

        End Try

        If (dgView.DataSource Is MySQLConnect.MonJeuDeDonnees.Tables("produit")) Then
            dgView.DataSource = Executer_Requete_Select("call rechProdMulti('" & TextBoxRech.Text & "','" & TextBoxRech2.Text & "')", "produit")

            Executer_Requete_Select("call selectFournisseurs()", "fournisseur")
            ComboBox5.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("fournisseur")
            ComboBox5.DisplayMember = "NomFournisseur"
            ComboBox5.ValueMember = "idFournisseur"

            Executer_Requete_Select("call selectCategories()", "categorie")
            ComboBox6.DataSource = MySQLConnect.MonJeuDeDonnees.Tables("categorie")
            ComboBox6.DisplayMember = "LibelleCategorie"
            ComboBox6.ValueMember = "idCategorie"
        Else

        End If
    End Sub
#End Region

    Private Sub ComboBoxClient_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxClient.SelectedIndexChanged
        Try
            MySQLConnect.MonJeuDeDonnees.Tables("client").Clear()
        Catch ex As Exception

        End Try
        If (ComboBoxClient.SelectedIndex = 0) Then
            dgView.DataSource = Executer_Requete_Select("select idClient, NomClient, PrenomClient, AdRueClient, AdCpClient, AdVilleClient, email from client, internaute where client.idClient = internaute.client_idClient", "client")
            LabelRech2.Text = "E-Mail"
            TextBoxRech2.Visible = True
        Else
            dgView.DataSource = Executer_Requete_Select("select idClient, NomClient, PrenomClient, AdRueClient, AdCpClient, AdVilleClient, poste from client, employe where client.idClient = employe.client_idClient", "client")
            LabelRech2.Text = "Poste"
            TextBoxRech2.Visible = True
        End If
    End Sub

    Private Sub ComboBoxType_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBoxType.SelectedIndexChanged
        Try
            MySQLConnect.MonJeuDeDonnees.Tables("commande").Clear()
        Catch ex As Exception

        End Try
        If (ComboBoxType.SelectedIndex = 0) Then
            ComboBox3.DataSource = Executer_Requete_Select("select idClient, NomClient, PrenomClient from client, employe where client.idClient = employe.client_idClient", "client")
            dgView.DataSource = Executer_Requete_Select("call getDetailCommandeInterne()", "commande")

        Else
            ComboBox3.DataSource = Executer_Requete_Select("select idClient, NomClient, PrenomClient from client, internaute where client.idClient = internaute.client_idClient", "client")
            dgView.DataSource = Executer_Requete_Select("call getDetailCommandeExterne()", "commande")
        End If
    End Sub
End Class
